package com.fan.teamer.process;

import org.apache.log4j.Logger;

import com.fan.teamer.global.TeamerGlobalVariables;
import com.fan.teamer.model.domain.Member;
import com.fan.teamer.model.domain.Team;
import com.fan.teamer.model.domain.TeamMember;
import com.fan.teamer.service.MemberService;
import com.fan.teamer.service.TeamMemberService;
import com.fan.teamer.service.TeamService;

public class TeamMemberUpdateProcess extends ProcExec<Integer> {

	private static Logger logger = Logger.getLogger("TeamMemberUpdateProcess");

	private Member operator;
	private Member member;
	private Team team;
	private MemberService memberService;
	private TeamService teamService;
	private TeamMemberService teamMemberService;

	private String role;

	public static Logger getLogger() {
		return logger;
	}

	public static void setLogger(Logger logger) {
		TeamMemberUpdateProcess.logger = logger;
	}

	public Member getOperator() {
		return operator;
	}

	public void setOperator(Member operator) {
		this.operator = operator;
	}

	public Member getMember() {
		return member;
	}

	public void setMember(Member member) {
		this.member = member;
	}

	public Team getTeam() {
		return team;
	}

	public void setTeam(Team team) {
		this.team = team;
	}

	public MemberService getMemberService() {
		return memberService;
	}

	public void setMemberService(MemberService memberService) {
		this.memberService = memberService;
	}

	public TeamService getTeamService() {
		return teamService;
	}

	public void setTeamService(TeamService teamService) {
		this.teamService = teamService;
	}

	public TeamMemberService getTeamMemberService() {
		return teamMemberService;
	}

	public void setTeamMemberService(TeamMemberService teamMemberService) {
		this.teamMemberService = teamMemberService;
	}

	public String getRole() {
		return role;
	}

	public void setRole(String role) {
		this.role = role;
	}

	public int initCheck() {

		if (memberService == null || teamService == null
				|| teamMemberService == null) {
			setErrorMessage("Service is null!");
			return TeamerGlobalVariables.FAILED;
		}
		if (operator == null || member == null || team == null) {
			setErrorMessage("Domain cannot be null");
			return TeamerGlobalVariables.FAILED;
		}
		if (!teamService.hasTeam(team.getId())) {
			setErrorMessage("Team: " + team.getName()
					+ " doesn't exist!");
			return TeamerGlobalVariables.FAILED;
		}
		if (!memberService.hasMember(member.getEmail())) {
			setErrorMessage("Member: " + member.getEmail() + " doesn't exist!");
			return TeamerGlobalVariables.FAILED;
		}
		if (!teamService.hasMember(team, member)) {
			setErrorMessage("Team: " + team.getName()
					+ " doesn't have member " + member.getEmail());
			return TeamerGlobalVariables.FAILED;
		}
		if (!teamService.hasMember(team, operator)) {
			setErrorMessage("Team: " + team.getName()
					+ " doesn't have member " + operator.getEmail());
			return TeamerGlobalVariables.FAILED;
		}

		return TeamerGlobalVariables.SUCCESS;

	}

	public int checkAutority() {

		// no one can change others to organiser
		if (role.equals(TeamerGlobalVariables.ORGANIZER)) {
			setErrorMessage("no one can change others to organiser ");
			return TeamerGlobalVariables.FAILED;
		}

		String operatorRole = teamMemberService.getTeamMember(team, operator)
				.getMemberRole();

		String memberRole = teamMemberService.getTeamMember(team, member)
				.getMemberRole();

		// organiser role cannot be changed anyway
		if (memberRole.equals(TeamerGlobalVariables.ORGANIZER)) {
			setErrorMessage("organiser role cannot be changed anyway ");
			return TeamerGlobalVariables.FAILED;
		}

		// organiser can set co-organisers and participants to all roles
		if (operatorRole.equals(TeamerGlobalVariables.ORGANIZER)) {
			return TeamerGlobalVariables.SUCCESS;
		}
		// co-organiser can set co-organiser and participant to co-organiser and
		// participant
		else if (operatorRole.equals(TeamerGlobalVariables.CO_ORGANIZER)) {
			return TeamerGlobalVariables.SUCCESS;
		} else {
			// participant cannot set role
			setErrorMessage("Participant cannot set role");
			return TeamerGlobalVariables.FAILED;
		}
	}

	@Override
	public Integer processImpl() {
		if (initCheck() == TeamerGlobalVariables.FAILED) {
			return TeamerGlobalVariables.FAILED;
		}

		if (checkAutority() == TeamerGlobalVariables.FAILED) {
			return TeamerGlobalVariables.FAILED;
		}
		TeamMember teamMember = teamMemberService.getTeamMember(team, member);
		teamMember.setMemberRole(role);

		try {
			teamMemberService.updateTeamMember(teamMember);
		} catch (Exception e) {
			e.printStackTrace();
			setErrorMessage("Member: " + member.getEmail()
					+ " cannot be updated in team " + team.getName());
			return TeamerGlobalVariables.FAILED;
		}

		return TeamerGlobalVariables.SUCCESS;
	}

}
